/*
* $Id: SyncWriteData.cpp 347 2012-02-29 19:58:23Z volok-aleksej@yandex.ru $
*
* Copyright 2009-2011 by Volokitin Aleksey
* All rights reserved
*
*/
#include "Common/include.h"
#include "SyncWriteData.h"
#include "SyncChooseComPort.h"
#include "SyncChooseData.h"
#include "LanguageManager.h"
#include "Threads/ComReader.h"

#define MOBILE_RADIO_ID 0x101
#define LOG_LISTBOX_ID	0x102

CSyncWriteData::CSyncWriteData(CWizardStep* pBack, CWnd* pParentWnd)
: CWizardStep(pBack, pParentWnd){
	m_fontHeader = CCommonFunction::GetFont(_T("Arial"), 16, true, false);
	m_font = CCommonFunction::GetFont(_T("Arial"), 16, false, false);
	m_pNext = NULL;
	m_pComReader = new CComReader(this, true);
}

CSyncWriteData::~CSyncWriteData(){
	m_pComReader->StopThread();
	delete m_pComReader;
	m_fontHeader->DeleteObject();
	delete m_fontHeader;
	m_font->DeleteObject();
	delete m_font;
	delete m_pNext;
}

void CSyncWriteData::Init(CPoint offset){
	HWND nextButton = GetDlgItem(m_pParentWnd->m_hWnd, ID_NEXT);
	EnableWindow(nextButton, FALSE);
	SetWindowText(nextButton, ILanguageManager::GetLanguageManager()->GetString(CLOSED_ID).c_str());

	CStatic* header1 = new CStatic;
	header1->Create(CLanguageManager::GetLanguageManager()->GetString(SYNC_HEADER_ID).c_str(),
		WS_CHILD | WS_VISIBLE | SS_CENTER, CRect(offset, CSize(470, 20)), m_pParentWnd);
	header1->SetFont(m_fontHeader);
	AddWindow(header1);
	CStatic* header2 = new CStatic;
	header2->Create(CLanguageManager::GetLanguageManager()->GetString(SYNC_WAIT_ID).c_str(),
		WS_CHILD | WS_VISIBLE | SS_LEFT, CRect(offset + CPoint(20, 20), CSize(450, 40)), m_pParentWnd);
	header2->SetFont(m_fontHeader);
	AddWindow(header2);
	m_log = new CListBox;
	m_log->Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER | LBS_COMBOBOX,
		CRect(offset + CPoint(20, 60), CSize(430, 190)), m_pParentWnd, LOG_LISTBOX_ID);
	m_log->SetFont(m_font);
	AddWindow(m_log);

	CSyncChooseComPort* pBack = dynamic_cast<CSyncChooseComPort*>(m_pBack);
	if(!pBack)
		return;
	std::tstring strComPort = pBack->GetComPort();
	strComPort.insert(0, _T("\\\\.\\"));
	m_pComReader->SetComFileName(strComPort);
	m_pComReader->CreateThread();
}

CWizardStep* CSyncWriteData::Next(){
	CloseWindows();
	m_pComReader->StopThread();
	return m_pNext;
}

CWizardStep* CSyncWriteData::Back(){
	CloseWindows();
	m_pComReader->StopThread();
	return m_pBack;
}

void CSyncWriteData::Update(){
	HWND nextButton = GetDlgItem(m_pParentWnd->m_hWnd, ID_NEXT);
	if(m_pComReader->IsStarted())
		EnableWindow(nextButton, FALSE);
	else
		EnableWindow(nextButton, TRUE);
}

void CSyncWriteData::SendNotify(const std::tstring& strNotify){
	m_log->AddString(strNotify.c_str());
}

void CSyncWriteData::SetWriteFamilies(const std::map<int, std::vector<int> >& aFamilies)
{
	m_pComReader->SetWriteFamilies(aFamilies);
}

void CSyncWriteData::SetInterval(time_t begin, time_t end)
{
	m_pComReader->SetInterval(begin, end);
}
